package com.fh.fruit.service.user.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.fh.dao.DaoSupport;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.fruit.service.user.UserManager;
import com.fh.fruit.util.CustomUtil;
import com.fh.util.Const;
import com.fh.util.DateUtil;
import com.fh.util.Jurisdiction;
import com.fh.util.MD5;
import com.fh.util.PageData;

@Service("fruitUserService")
public class UserService implements UserManager {

	@Resource(name = "daoSupport")
	private DaoSupport dao;

	// 用户注册
	@Override
	public void saveUser(PageData pd) throws Exception {
		// TODO Auto-generated method stub
		String phone = pd.getString("phone");
		String pwd = pd.getString("pwd");
		pd.put("pwd", MD5.md5(pwd));
		pd.put("userName", phone);
		dao.save("FruitUserMapper.insertUserLogin", pd);
		System.out.println("登录id===============" + pd.get("id"));
		String loginId = pd.get("id").toString();
		String petName = CustomUtil.subPhone(phone);
		pd.put("petName", petName);
		pd.put("loginId", loginId);
		pd.put("phone", phone);
		pd.put("companyName", "");
		pd.put("address", "");
		pd.put("creationDate", "");
		pd.put("isAuthentication", "01");
		User user = (User) Jurisdiction.getSession().getAttribute(Const.SESSION_USER);
		pd.put("codePre", DateUtil.getYear());// 前缀
		pd.put("codeSuf", CustomUtil.buildRandom(3) + "");// 后缀
		dao.save("FruitUserMapper.insertUserInfo", pd);
		System.out.println("用户id======================" + pd.get("id"));
		String userId = pd.get("id").toString();
		pd.put("userId", userId);
		pd.put("loginId", loginId);
		dao.save("FruitUserMapper.insertUserAccount", pd);
	}

	/**
	 * 保存用户
	 * 
	 * @param pd
	 * @
	 */
	public void saveU(PageData pd) throws Exception {
		dao.save("FruitUserMapper.insertUserInfo", pd);
	}

	/**
	 * 添加登录账号
	 * 
	 * @param pd
	 * @throws Exception
	 */
	public void savaLogin(PageData pd) throws Exception {
		dao.save("FruitUserMapper.insertUserLogin", pd);
	}

	/**
	 * 添加账户信息
	 * 
	 * @param pd
	 * @throws Exception
	 */
	public void savaAccount(PageData pd) throws Exception {
		dao.save("FruitUserMapper.insertUserAccount", pd);
	}

	/**
	 * 修改用户
	 * 
	 * @param pd
	 * @
	 */
	public void editU(PageData pd) throws Exception {
		dao.update("FruitUserMapper.editU", pd);
	}

	/**
	 * 删除用户
	 * 
	 * @param pd
	 * @
	 */
	public void deleteU(PageData pd) throws Exception {
		dao.delete("FruitUserMapper.deleteU", pd);
	}

	/**
	 * 批量删除用户
	 * 
	 * @param USER_IDS
	 * @
	 */
	public void deleteAllU(String[] USER_IDS) throws Exception {
		dao.delete("FruitUserMapper.deleteAllU", USER_IDS);
	}

	/**
	 * 用户列表(全部)
	 * 
	 * @param USER_IDS
	 * @
	 */
	@SuppressWarnings("unchecked")
	public List<PageData> listAllUser(PageData pd) throws Exception {
		return (List<PageData>) dao.findForList("FruitUserMapper.listAllUser", pd);
	}

	/**
	 * 会员列表
	 * 
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<PageData> listfruitUsers(Page page) throws Exception {
		return (List<PageData>) dao.findForList("FruitUserMapper.fruitUserlistPage", page);
	}

	@Override
	public void updateUserState(PageData pd) throws Exception {
		// TODO Auto-generated method stub
		dao.update("FruitUserMapper.updateUserState", pd);
	}
}
